<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Promotion;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Promotion\Ctl\AdRewardConfCtl;
use Srv\Apps\Proto\DTO\Promotion\AdRewardConfAddDTO;
use Srv\Apps\Proto\DTO\Promotion\AdRewardConfEditDTO;
use Srv\Apps\Proto\DTO\Promotion\AdRewardConfListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Promotion\AdRewardConfListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class AdRewardConfAct extends ActionAbstract
{
    /**
     * @url /ad/reward/conf/list
     * @auth adRewardConfList
     * @req Srv.Apps.Proto.DTO.Promotion.AdRewardConfListDTO
     * @rsp Srv.Apps.Proto.VO.Promotion.AdRewardConfListVO
     * 广告奖励配置列表
     */
    public function getAdRewardConfList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof AdRewardConfListDTO) || !($VOClass instanceof AdRewardConfListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = AdRewardConfCtl::tag()->getAdRewardConfList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /ad/reward/conf/add
     * @auth adRewardConfAdd
     * @req Srv.Apps.Proto.DTO.Promotion.AdRewardConfAddDTO
     * 广告奖励配置新增
     */
    public function adRewardConfAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof AdRewardConfAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData    = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl     = LogCtl::tag();
        $logData    = $setData;
        $adrcId     = AdRewardConfCtl::tag()->adRewardConfAdd($setData);
        if($adrcId < 1){
            $errMsg = '广告奖励配置新增失败';
            $LogCtl->logAdd(LogCtl::AD_REWARD_CONF_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg     = '广告奖励配置新增成功';
        $LogCtl->logAdd(LogCtl::AD_REWARD_CONF_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /ad/reward/conf/edit
     * @auth adRewardConfEdit
     * @req Srv.Apps.Proto.DTO.Promotion.AdRewardConfEditDTO
     * 广告奖励配置编辑
     */
    public function adRewardConfEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof AdRewardConfEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $adrcId             = intval($DTOClass->getAdrcId());
        $setData            = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl             = LogCtl::tag();
        $AdRewardConfCtl    = AdRewardConfCtl::tag();
        $adRewardConfSrc    = $AdRewardConfCtl->getAdRewardConfSrc($adrcId);
        if(!isset($adRewardConfSrc['adrc_id'])){
            $logData        = $setData;
            $errMsg         = '广告奖励配置不存在';
            $LogCtl->logAdd(LogCtl::AD_REWARD_CONF_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result             = $AdRewardConfCtl->adRewardConfEdit($adrcId, $setData);
        $logData            = ['new' => $setData, 'old' => $adRewardConfSrc];
        if(!$result){
            $errMsg         = '广告奖励配置编辑失败';
            $LogCtl->logAdd(LogCtl::AD_REWARD_CONF_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg             = '广告奖励配置编辑成功';
        $LogCtl->logAdd(LogCtl::AD_REWARD_CONF_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}